package com.glasgowrax.domain.directions;

import org.json.JSONException;
import org.json.JSONObject;

import com.glasgowrax.domain.LatLng;

public class TransitDetails {

	private long arrivalTimeSeconds;
	private String arrivalTimeDescription;
	private String arrivalStopName;
	private LatLng arrivalPoint;
	
	private long departureTimeSeconds;
	private String departureTimeDescription;
	private String departureStopName;
	private LatLng departurePoint;
	
	private String headsign;
	private long headway;
	private int numberOfStops;
	
	private Line line;
	
	public TransitDetails(JSONObject json) {
		
		try {
			JSONObject arrivalStop = (json.has("arrival_stop")) ? json.getJSONObject("arrival_stop") : new JSONObject();
			JSONObject arrivalStopLocation = (arrivalStop.has("location")) ? arrivalStop.getJSONObject("location") : new JSONObject();
			JSONObject arrivalTime = (json.has("arrival_time")) ? json.getJSONObject("arrival_time") : new JSONObject();
			JSONObject departureStop = (json.has("departure_stop")) ? json.getJSONObject("departure_stop") : new JSONObject();
			JSONObject departureStopLocation = (departureStop.has("location")) ? departureStop.getJSONObject("location") : new JSONObject();
			JSONObject departureTime = (json.has("departure_time")) ? json.getJSONObject("departure_time") : new JSONObject();
			JSONObject jsonLine = (json.has("line")) ? json.getJSONObject("line") : new JSONObject();
			
			arrivalTimeSeconds = (arrivalTime.has("value")) ? arrivalTime.getLong("value") : -1;
			arrivalTimeDescription = (arrivalTime.has("text")) ? arrivalTime.getString("text") : "";
			arrivalStopName = (arrivalStop.has("name")) ? arrivalStop.getString("name") : "";
			arrivalPoint = (arrivalStopLocation.has("lat") && arrivalStopLocation.has("lng")) ? new LatLng(arrivalStopLocation.getDouble("lat"), arrivalStopLocation.getDouble("lng")) : null;
			
			departureTimeSeconds = (departureTime.has("value")) ? departureTime.getLong("value") : -1;
			departureTimeDescription = (departureTime.has("text")) ? departureTime.getString("text") : "";
			departureStopName = (departureStop.has("name")) ? departureStop.getString("name") : "";
			departurePoint = (departureStopLocation.has("lat") && departureStopLocation.has("lng")) ? new LatLng(departureStopLocation.getDouble("lat"), departureStopLocation.getDouble("lng")) : null;
						
			headsign = (json.has("headsign")) ? json.getString("headsign") : "";
			headway = (json.has("headway")) ? json.getLong("headway") : -1;
			numberOfStops = (json.has("numberOfStops")) ? json.getInt("numberOfStops") : -1;
			
			line = new Line(jsonLine);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
	}

	public long getArrivalTimeSeconds() {
		return arrivalTimeSeconds;
	}

	public void setArrivalTimeSeconds(long arrivalTimeSeconds) {
		this.arrivalTimeSeconds = arrivalTimeSeconds;
	}

	public String getArrivalTimeDescription() {
		return arrivalTimeDescription;
	}

	public void setArrivalTimeDescription(String arrivalTimeDescription) {
		this.arrivalTimeDescription = arrivalTimeDescription;
	}

	public String getArrivalStopName() {
		return arrivalStopName;
	}

	public void setArrivalStopName(String arrivalStopName) {
		this.arrivalStopName = arrivalStopName;
	}

	public LatLng getArrivalPoint() {
		return arrivalPoint;
	}

	public void setArrivalPoint(LatLng arrivalPoint) {
		this.arrivalPoint = arrivalPoint;
	}

	public long getDepartureTimeSeconds() {
		return departureTimeSeconds;
	}

	public void setDepartureTimeSeconds(long departureTimeSeconds) {
		this.departureTimeSeconds = departureTimeSeconds;
	}

	public String getDepartureTimeDescription() {
		return departureTimeDescription;
	}

	public void setDepartureTimeDescription(String departureTimeDescription) {
		this.departureTimeDescription = departureTimeDescription;
	}

	public String getDepartureStopName() {
		return departureStopName;
	}

	public void setDepartureStopName(String departureStopName) {
		this.departureStopName = departureStopName;
	}

	public LatLng getDeparturePoint() {
		return departurePoint;
	}

	public void setDeparturePoint(LatLng departurePoint) {
		this.departurePoint = departurePoint;
	}

	public String getHeadsign() {
		return headsign;
	}

	public void setHeadsign(String headsign) {
		this.headsign = headsign;
	}

	public long getHeadway() {
		return headway;
	}

	public void setHeadway(long headway) {
		this.headway = headway;
	}

	public int getNumberOfStops() {
		return numberOfStops;
	}

	public void setNumberOfStops(int numberOfStops) {
		this.numberOfStops = numberOfStops;
	}

	public Line getLine() {
		return line;
	}

	public void setLine(Line line) {
		this.line = line;
	}
	
	
	
}
